package com.easyoffer.config;

import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

@Configuration
@Slf4j
public class NacosConfig {
    @Value("${spring.cloud.nacos.config.server-addr}")
    public String serverAddr;
    @Value("${spring.cloud.nacos.config.namespace}")
    public String namespace;
    @Bean
    public ConfigService configService(){
        try {
            Properties properties = new Properties();
            properties.put("serverAddr", serverAddr);
            properties.put("namespace", namespace); //命名空间不要用public
            return NacosFactory.createConfigService(properties);
//            return NacosFactory.createConfigService(serverAddr);
        } catch (NacosException e) {
            log.info("创建ConfigService失败");
            throw new IllegalArgumentException("创建ConfigService失败");
        }
    }

    @Bean
    public NamingService namingService(){
        try {
            Properties properties = new Properties();
            properties.put("serverAddr", serverAddr);
            properties.put("namespace", namespace);
            return  NacosFactory.createNamingService(properties);
        } catch (NacosException e) {
            log.info("创建ConfigService失败");
            throw new IllegalArgumentException("创建ConfigService失败");
        }
    }

}
